package solution;

public class Solution {
    //买卖股票含冷冻期
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[] f = new int[n];
        int[] g = new int[n];
        int[] k = new int[n];
        f[0] = -prices[0];
        g[0] = 0;
        k[0] = 0;
        for (int i = 1; i < n; i++) {
            f[i] = Math.max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = Math.max(g[i - 1], k[i - 1]);
            k[i] = f[i - 1] + prices[i];
        }
        return Math.max(g[n - 1], k[n - 1]);
    }

    //买卖股票包含手续费
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = -prices[0];
        g[0] = 0;
        for (int i = 1; i < n; i++) {
            f[i] = Math.max(f[i - 1], g[i - 1] - prices[i]);
            g[i] = Math.max(f[i - 1] + prices[i] - fee, g[i - 1]);
        }
        return g[n - 1];
    }
}
